<template>
  <div class="header_container">
    <div>
      <div class="header_icon"></div>
      <div class="header_title">{{ sysTitle }}</div>
    </div>
    <div class="header_userinfo">
      <!-- 菜单搜索 -->
      <el-select
        v-model="searchValue"
        placeholder="全局搜索"
        style="width: 280px"
        filterable
        clearable
        @change="changeMenu"
      >
        <template #prefix>
          <el-icon class="el-input__icon"><search /></el-icon>
        </template>
        <el-option
          v-for="item in menuOption"
          :key="item.menuId"
          :label="item.path.map((i) => i.menuName).join('/')"
          :value="item.menuId"
        >
          <span style="float: left">{{ item.menuName }}</span>
          <span style="float: right; color: var(--el-text-color-secondary); font-size: 13px">
            {{ item.path.map((i) => i.menuName).join('/') }}
          </span>
        </el-option>
      </el-select>
      <el-divider direction="vertical" />
      <!-- 用户信息 -->
      <el-dropdown @command="handleCommand">
        <div class="custom-dropdown">
          <el-icon :size="14"> <UserFilled /></el-icon>
          {{ userdatainfo.userName }}
        </div>
        <template #dropdown>
          <el-dropdown-menu>
            <el-dropdown-item command="updatePassword">修改密码</el-dropdown-item>
            <el-dropdown-item command="exit">退出登录</el-dropdown-item>
          </el-dropdown-menu>
        </template>
      </el-dropdown>

      <el-divider direction="vertical" />
      <!-- 暗黑模式 -->
      <el-tooltip content="切换主题">
        <el-button
          :icon="visionStore.$state.isDark ? 'Sunny' : 'Sunrise'"
          link
          @click="visionStore.setIsDark()"
        />
      </el-tooltip>
      <!-- 刷新当前菜单 -->
      <el-tooltip content="刷新当前菜单">
        <el-button icon="Refresh" link @click="refreshMenu" />
      </el-tooltip>
    </div>
  </div>
</template>

<script setup>
import router from '@/router'
import { ElMessage, ElMessageBox } from 'element-plus'
import { onMounted, ref, watch } from 'vue'
import { useUserStore } from '@/stores/modeles/user'
import { useRouterStore } from '@/stores/modeles/router'
import { useVisionStore } from '@/stores/modeles/vision'

const routerStore = useRouterStore()
const sysTitle = ref('')
const userStore = useUserStore()
const userdatainfo = ref({})
const searchValue = ref('')
const menuOption = ref([])
const visionStore = useVisionStore()

//查询菜单跳转
const changeMenu = (val) => {
  if (val && val != '') {
    //跳转
    const path = menuOption.value.filter((i) => i.menuId == val)[0].menuUrl
    router.push({ path })
  }
}

//用户信息操作
const handleCommand = (command) => {
  const handelData = {
    // updatePassword: exitLogin(),
    exit: exitLogin(),
  }
  handelData[command] && handelData[command]()
}

//刷新菜单
const refreshMenu = () => {
  routerStore.refreshMenu()
}

//退出登录
const exitLogin = () => {
  ElMessageBox.confirm('确认退出登录?', '警告', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning',
  })
    .then(() => {
      userStore.logout().then((res) => {})
    })
    .catch(() => {})
}

onMounted(() => {
  sysTitle.value = COMMON_COJNFIG.SYS_TITLE
  menuOption.value = routerStore.$state.menuListNotree.filter((i) => !i.children)
})

watch(
  () => userStore.$state.userInfo,
  (val) => {
    userdatainfo.value = val
  }
)
</script>
    
<style scoped lang="scss">
.header_container {
  width: 100%;
  height: 48px;
  box-shadow: 0px 4px 10px 0px rgba(78, 89, 105, 0.06);
  background-color: var(--sys-bg-color);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 16px;
  font-family: PingFangSC;
  position: relative;
  z-index: 2;

  > div {
    display: flex;
    align-items: center;
    gap: 8px;
  }

  .header_icon {
    width: 32px;
    height: 32px;
    background: url('@/assets/dashboard/headersvg.svg') no-repeat;
    background-size: 100% 100%;
  }

  .header_userinfo {
    font-size: 14px;
    gap: 8px;

    .custom-dropdown:focus-visible {
      outline: unset;
    }

    .custom-dropdown {
      cursor: pointer;
      gap: 4px;
      border: none !important;
      display: flex;
      align-items: center;
    }

    :deep(.el-button) {
      padding: 0px;
      margin: 0px !important;
    }

    :deep(.el-select__wrapper) {
      border-radius: 16px;
    }

    // :deep(.el-select__suffix) {
    //   display: none;
    // }
  }

  .header_user {
    width: 36px;
    height: 36px;
    border-radius: 18px;
    background-color: #409eff;
    color: var(--sys-bg-color);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 16px;
    cursor: pointer;
  }

  .header_title {
    font-size: 20px;
    font-weight: normal;
    letter-spacing: normal;
  }
}
</style>